package com.lxl.sort;

import java.util.Stack;

/**
 * @Author lixilin
 * @Date 2021/12/25 22:34
 * @Description
 */
public class QuicklySort {
    public static void main(String[] args) {
        int[] arr = {42, 12, 2, 3, 6, 25, 12, 28};
        qSort(arr, 0, arr.length - 1);
        for (int i : arr) {
            System.out.println(" " + i);
        }

    }

    public static void quickSort(int[] arr, int startIndex, int endIndex) {
        int l = startIndex;
        int r = endIndex;
        // 中轴值
        int pivot = arr[(startIndex + endIndex) / 2];
        // 交换时保存值
        int temp = 0;
        while (l < r) {
            // 如果左边的值小于中轴值则接着往右移动
            while (arr[l] < pivot) {
                l++;
            }
            // 如果右边的值大于中轴值则往左移动
            while (arr[r] > pivot) {
                r--;
            }
            // l >= r说明左边全部是比中轴值小的值
            if (l >= r) {
                break;
            }
            // 交换
            temp = arr[l];
            arr[l] = arr[r];
            arr[r] = temp;

            // 交换完成后,分别用左边索引的值和右边索引的值和中轴值进行比较
            if (arr[l] == pivot) {
                r--;
            }
            if (arr[r] == pivot) {
                l++;
            }
            // 如果 l == r,必须 进行移动,否则容易导致栈溢出
            if (l == r) {
                l++;
                r--;
            }
            // 左递归
            if (startIndex < r) {
                quickSort(arr, startIndex, r);
            }
            // 右递归
            if (l < endIndex) {
                quickSort(arr, l, endIndex);
            }
        }
    }

    /**
     * 非递归
     * @param arr
     * @param left
     * @param right
     */
    public static void qSort(int[] arr, int left, int right) {
        int pivot;
        if (left >= right) {
            return;
        }
        Stack<Integer> stack = new Stack<Integer>();
        stack.push(left);
        stack.push(right);
        while (!stack.isEmpty()) {
            right = stack.pop();
            left = stack.pop();
            pivot = partition(arr,left,right);
            if (left < (pivot-1)) {
                stack.push(left);
                stack.push(pivot - 1);
            }
            if ((pivot+1) < right) {
                stack.push(pivot + 1);
                stack.push(right);
            }
        }
    }

    private static int partition(int[] arr, int left, int right) {
        if (arr[left] > arr[right]) {
            swap(arr, left, right);
        }
        if (arr[(left+right)/2] > arr[right]) {
            swap(arr,(left+right)/2,right);
        }
        if (arr[left] < arr[(left+right)/2]) {
            swap(arr,left,(left+right)/2);
        }
        int pivot = arr[left];
        while (left < right) {
            while (left < right && arr[right] >= pivot) {
                right --;
            }
            arr[left] = arr[right];
            while (left < right && arr[left] <= pivot) {
                left++;
            }
            arr[right] = arr[left];
        }
        arr[left] = pivot;
        return left;
    }

    private static void swap(int[] arr, int left, int right) {
        int temp = arr[left];
        arr[left] = arr[right];
        arr[right] = temp;
    }
}
